package com.example.teamtribe.main.club.post;

import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.PhotoActivity;
import com.example.teamtribe.common.PopupPics;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TImage;
import com.jph.takephoto.model.TResult;

import java.util.ArrayList;
import java.util.List;

public class PostActivity extends PhotoActivity implements View.OnClickListener, PostContract.View, PhotosAdapter.Callback, PopupPics.OnPickListener {

    private EditText etTitle;
    private EditText etContent;
    private AppCompatSpinner spiType;
    private PostContract.Presenter presenter;
    private List<String> images;
    private PhotosAdapter adapter;
    private int teamId;
    private PopupPics popupPics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        teamId = getIntent().getIntExtra("teamId", 0);
        images = new ArrayList<>();
        images.add(null);
        adapter = new PhotosAdapter(images);
        adapter.setListener(this);
        popupPics = new PopupPics(this, this);
        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        spiType = findViewById(R.id.spi_type);
        if (teamId == 0)
            spiType.setVisibility(View.GONE);
        RecyclerView recyclerView = findViewById(R.id.rv_photos);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.setAdapter(adapter);
        presenter = new PostPresenter(this);
        findViewById(R.id.fab_yes).setOnClickListener(this);
        if (getIntent().getIntExtra("type", 0) != 0) {
            ((TextView) findViewById(R.id.tv_title)).setText("发布大型活动");
            return;
        }
        ((TextView) findViewById(R.id.tv_title)).setText("发表帖子");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fab_yes:
                presenter.post(teamId, etTitle.getText().toString().trim(),
                        etContent.getText().toString().trim(), spiType.getSelectedItemPosition(), images);
                break;
            default:
                break;
        }
    }

    @Override
    public void next() {
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void takeSuccess(TResult result) {
        List<TImage> resultImages = result.getImages();
        for (TImage tImage : resultImages) {
            images.add(images.size() - 1, tImage.getOriginalPath());
        }
        if (images.size() == 10) {
            images.remove(null);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onPick(View v) {
        popupPics.show(v);
    }

    @Override
    public void toDelete() {
        adapter.setShowDelete(true);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void notifyDataSetChanged(final int position) {
        images.remove(position);
        final boolean flag = !images.contains(null);
        if (flag) {
            images.add(null);
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyItemRemoved(position);
                adapter.notifyItemRangeChanged(position, images.size() - position);
                if (flag) {
                    adapter.notifyItemInserted(images.size() - 1);
                    adapter.notifyItemRangeChanged(images.size() - 1, 1);
                }
            }
        });
    }

    @Override
    public void fromGallery() {
        CropOptions cropOptions = new CropOptions.Builder().setWithOwnCrop(false).create();
        getTakePhoto().onPickFromGalleryWithCrop(getUri(), cropOptions);
        //getTakePhoto().onPickMultiple(9);//10 - images.size());
    }

    @Override
    public void fromCapture() {
        CropOptions cropOptions = new CropOptions.Builder().setWithOwnCrop(false).create();
        getTakePhoto().onPickFromCaptureWithCrop(getUri(), cropOptions);
    }
}
